<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="btn_wrapper">
          <span
            style="font-size: 12px; color: pink"
          >说明：目前支持学科和关键字条件筛选</span>
          <!-- 添加按钮 -->
          <el-button
            class="button-add"
            size="small"
            type="success"
            @click="$router.push('/questions/new')"
          >
            <i class="el-icon-edit" />
            新增试题
          </el-button>
        </div>

        <!-- form表单 筛选区域 -->
        <el-form ref="form" :model="form" label-width="80px" size="small">
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                  v-model="form.subjectID"
                  size="small"
                  placeholder="请选择"
                  style="width: 100%"
                  @change="getDirectoryList(form.subjectID)"
                >
                  <el-option
                    v-for="obj in simpleList"
                    :key="obj.value"
                    :label="obj.label"
                    :value="obj.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  v-model="form.catalogID"
                  style="width: 100%"
                  size="small"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="obj in directorysList"
                    :key="obj.value"
                    :label="obj.label"
                    :value="obj.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select
                  v-model="form.tags"
                  style="width: 100%"
                  size="small"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="obj in tagesList"
                    :key="obj.value"
                    :label="obj.label"
                    :value="obj.label"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input v-model="form.keyword" size="small" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="题目类型">
                <el-select
                  v-model="form.questionType"
                  style="width: 100%"
                  size="small"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="obj in questionType"
                    :key="obj.value"
                    :label="obj.label"
                    :value="obj.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select
                  v-model="form.difficulty"
                  style="width: 100%"
                  size="small"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="obj in difficulty"
                    :key="obj.value"
                    :label="obj.label"
                    :value="obj.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select
                  v-model="form.direction"
                  style="width: 100%"
                  size="small"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in direction"
                    :key="item.index"
                    :label="item"
                    value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select
                  v-model="form.creatorID"
                  style="width: 100%"
                  size="small"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="obj in simpleUserList"
                    :key="obj.id"
                    :label="obj.username"
                    :value="obj.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input v-model="form.remarks" size="small" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input v-model="form.shortName" size="small" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-select
                  v-model="form.province"
                  class="citySelect"
                  placeholder="请选择"
                  size="small"
                  @change="getCity(form.province)"
                >
                  <el-option
                    v-for="(item, index) in provinces"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
                <el-select
                  v-model="form.city"
                  class="areaSelect"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="(item, index) in citys"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="text-align: right">
                <el-button @click="clsButton">清除</el-button>
                <el-button type="primary" @click="searchButton">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <!-- 列表区域 -->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部" name="first">
            <!-- 精选题目列表组件 -->
            <questionList
              :search-result="searchResult"
              @getQuestion="searchButton"
              @Changepage="Changepage"
            />
          </el-tab-pane>
          <el-tab-pane label="待审核" name="second">
            <!-- 精选题目列表组件 -->
            <questionList
              :search-result="searchResult"
              @getQuestion="searchButton"
              @Changepage="Changepage"
            />
          </el-tab-pane>
          <el-tab-pane label="已审核" name="third">
            <!-- 精选题目列表组件 -->
            <questionList
              :search-result="searchResult"
              @getQuestion="searchButton"
              @Changepage="Changepage"
            />
          </el-tab-pane>
          <el-tab-pane label="已拒绝" name="fourth">
            <!-- 精选题目列表组件 -->
            <questionList
              :search-result="searchResult"
              @getQuestion="searchButton"
              @Changepage="Changepage"
            />
          </el-tab-pane>
        </el-tabs>
        <!-- 分页 -->

        <div class="pages">
          <el-pagination
            background
            :current-page="Number(form.page)"
            :total="Number(total)"
            :page-size="Number(form.pagesize)"
            :page-sizes="[5, 10, 15, 20]"
            layout="prev,pager,next,sizes,jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <!-- end -->
      </el-card>
    </div>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { simple as simpleDirectorys } from '@/api/hmmm/directorys'
import { simple as tagsListAPI } from '@/api/hmmm/tags'
import { simple as simpleUserList } from '@/api/base/users'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { provinces, citys } from '@/api/hmmm/citys'
import { choice } from '@/api/hmmm/questions'

// 组件
import questionList from './questions-choice/questionList.vue'
export default {
  name: 'QuestionsChoice',
  components: {
    questionList
  },
  data() {
    return {
      form: {
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签名称
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 城市
        city: null, // 区域
        page: 1,
        pagesize: 5
      },
      activeName: 'first', // tab栏选中
      simpleList: [], // 简单学科列表
      directorysList: [], // 简单二级目录列表
      tagesList: [], // 简单标签列表
      questionType: questionType, // 试题类型
      difficulty: difficulty, // 难度
      direction: direction, // 方向
      simpleUserList: [], // 简单用户列表
      provinces: [],
      citys: [], // 城市
      searchResult: {}, // 搜索结果
      total: '', // 数据总数量
      tabsSarchList: {} // 传给tab切换后子组件列表
    }
  },

  created() {
    // 获取学科列表
    this.subjectsList()

    // 获取简单列表
    this.getsimpleUserList()

    // 获取省份
    this.provinces = provinces()

    // 获取精选题库列表
    this.getChoiceQuestion()
  },

  methods: {
    // 获取学科列表
    async subjectsList() {
      const res = await simple()
      // console.log(res)
      this.simpleList = res.data
    },

    // 学科内容改变 获取二级目录列表\标签列表
    async getDirectoryList(subjectID) {
      // 清空二级目录
      this.form.catalogID = null

      // 目录列表
      const res = await simpleDirectorys({ subjectID })
      // console.log(res)
      this.directorysList = res.data
      // 标签列表
      const res2 = await tagsListAPI({ subjectID })
      // console.log(res2)
      this.tagesList = res2.data
    },

    // 简单用户列表
    async getsimpleUserList() {
      const res = await simpleUserList()
      // console.log(res)
      this.simpleUserList = res.data
    },

    // 省份变化 获取城市
    getCity(province) {
      this.form.city = ''
      this.citys = citys(province)
    },

    // 筛选表单清除按钮
    clsButton() {
      this.form = {
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签名称
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null,
        city: null,
        page: 1,
        pagesize: 5
      }
      // 清空二级目录下拉列表
      this.directorysList = []
      // 获取精选题库列表
      this.getChoiceQuestion()
    },

    // 获取精选题库列表
    async getChoiceQuestion() {
      // 调接口 获取精选题库列表
      const res = await choice(this.form)
      // console.log(res)
      this.searchResult = res.data

      // 总数量
      this.total = res.data.counts
    },

    // 筛选表单搜索按钮
    // 子组件刷新 重新获取列表
    searchButton() {
      // 获取精选题库列表
      this.getChoiceQuestion()
    },

    // 删除题目时 判断是否改变页码
    Changepage() {
      // 判断一页内数据 跳转页码
      if (this.searchResult.items.length === 1) {
        this.form.page--
        if (this.form.page <= 1) {
          this.form.page = 1
        }
      }
      this.getChoiceQuestion()
    },

    // 点击切换tab
    handleClick(tab, event) {
      // 回到第一页
      this.form.page = 1

      if (tab.index === '1') {
        // 待审核
        this.form.chkState = 0
      } else if (tab.index === '2') {
        // 已审核
        this.form.chkState = 1
      } else if (tab.index === '3') {
        // 已拒绝
        this.form.chkState = 2
      } else {
        this.form.chkState = null
      }
      this.getChoiceQuestion()
    },

    // 分页 每页显示几条数据改变触发
    handleSizeChange(val) {
      // 每页几条
      this.form.pagesize = val
      // 获取精选题库列表
      this.getChoiceQuestion()
    },

    // 分页 改变当前页码触发
    handleCurrentChange(val) {
      // 当前页
      this.form.page = val
      // 获取精选题库列表
      this.getChoiceQuestion()
    }
  }
}
</script>

<style scoped>
.btn_wrapper {
  margin-bottom: 15px;
  display: flex;
  justify-content: space-between;
}
.citySelect {
  width: 48%;
  margin-right: 4%;
}
.areaSelect {
  width: 48%;
}
</style>
